package com.xuexiang.xuidemo.slice.status.adapter;

import com.xuexiang.xui_lib.component.status.StatefulLayout;
import com.xuexiang.xui_lib.component.status.StatusLoader;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.element.ShapeElement;

public class SingleViewAdapter implements StatusLoader.Adapter {

    @Override
    public Component getView(StatusLoader.Holder holder, Component convertView, int status) {
        StatefulLayout statefulLayout = null;
        // 进行复用
        if (convertView instanceof StatefulLayout) {
            statefulLayout = (StatefulLayout) convertView;
        }
        if (statefulLayout == null) {
            statefulLayout = new StatefulLayout(holder.getContext());
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(new RgbColor(238, 243, 247));
            statefulLayout.setBackground(shapeElement);
            statefulLayout.attachTemplate();
            statefulLayout.setAnimatorEnable(false);
            statefulLayout.setLayoutConfig(new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_PARENT));
        }
        statefulLayout.setVisibility(status == StatusLoader.STATUS_LOAD_SUCCESS ? Component.HIDE : Component.VISIBLE);
        switch (status) {
            case StatusLoader.STATUS_LOADING:
                statefulLayout.showLoading();
                break;
            case StatusLoader.STATUS_LOAD_FAILED:
                statefulLayout.showError(holder.getRetryListener());
                break;
            case StatusLoader.STATUS_EMPTY_DATA:
                statefulLayout.showEmpty();
                break;
            case StatusLoader.STATUS_CUSTOM:
                statefulLayout.showOffline(holder.getRetryListener());
                break;
            default:
                break;
        }
        return statefulLayout;
    }
}
